<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-alert
        title="添加商品信息"
        type="warning"
        :closeable="false"
        show-icon
      >
      </el-alert>
      <!-- 减去-0是因为 v-model绑定name是string -0转化为num -->
      <el-steps
        :space="200"
        :active="activeIndex - 0"
        finish-status="success"
        align-center
      >
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <!-- 左边导航菜单 -->
      <el-form
        ref="addformRef"
        :model="addform"
        :rules="addformRules"
        label-width="80px"
        label-position="top"
      >
        <el-tabs
          :tab-position="tabPosition"
          v-model="activeIndex"
          :before-leave="beforeLeave"
          @tab-click="tabClick"
        >
          <el-tab-pane label="基本信息" name="0"
            >基本信息
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="addform.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input v-model="addform.goods_price" type="number"> </el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input v-model="addform.goods_weight" type="number">
              </el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input v-model="addform.goods_number" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品分类" prop="goods_cat">
              <el-cascader
                v-model="addform.goods_cat"
                :options="catelist"
                :props="cateProps"
                @change="handleChange"
              ></el-cascader>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="静态属性" name="1">
            <el-form-item
              :label="item.attr_name"
              v-for="item in manyTableData"
              :key="item.attr_id"
            >
              <el-checkbox-group v-model="item.attr_vals">
                <el-checkbox
                  border
                  :label="cb"
                  v-for="(cb, i) in item.attr_vals"
                  :key="i"
                ></el-checkbox>

                <el-checkbox label="选中且禁用" disabled></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="动态参数" name="2">
            <el-form-item
              :label="item.attr_name"
              v-for="item in onlyTableData"
              :key="item.attr_id"
            >
              <el-checkbox-group v-model="item.attr_vals">
                <el-checkbox
                  border
                  :label="cb"
                  v-for="(cb, i) in item.attr_vals"
                  :key="i"
                ></el-checkbox>

                <el-checkbox label="选中且禁用" disabled></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片" name="3">商品图片</el-tab-pane>
          <el-tab-pane label="商品内容" name="4">商品内容</el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 标签栏index
      activeIndex: 0,
      tabPosition: "left",
      // 添加商品的表单数据对象
      addform: {
        goods_name: "",
        goods_price: 0,
        goods_weight: 0,
        goods_number: 0,
        goods_cat: [],
      },
      // 页面加载获取商品分类数据
      catelist: [],
      cateProps: {
        expandTrigger: "hover",
        label: "cat_name",
        value: "cat_id",
        children: "children",
      },
      // 标签页2 的数据
      manyTableData: [],//静态属性
      onlyTableData:[],//动态参数

      addformRules: {
        goods_name: [
          { required: true, message: "请输入商品名", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "请输入商价格", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商重量", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" },
        ],
        goods_cat: [
          { required: true, message: "请选择商品分类", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    //获取商品分类数据方法
    async getCateList() {
      const { data: res } = await this.$http.get("categories");
      console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品数据失败");
      }
      this.catelist = res.data;
      console.log(this.catelist);
    },
    // 级联选择器
    handleChange() {
      console.log(this.addform.goods_cat);
      // 限制级联选择器只能选择3级标签
      if (this.addform.goods_cat.length !== 3) {
        this.addform.goods_cat = [];
      }
    },
    // 标签栏跳转触发事件
    beforeLeave(activeName, oldActiveName) {
      console.log(
        "即将离开的标签页",
        activeName,
        "要去的标签页",
        oldActiveName
      );
      if (oldActiveName == "0" && this.addform.goods_cat.length !== 3) {
        this.$message.error("请先选择商品分类哦");
        return false;
      }
    },
    // 标签栏2  请求数据 事件
    async tabClick() {
      console.log("打印当前标签栏的name", this.activeIndex);
      // 判断进入动态参数面板  根据index来发送不同的请求
      if (this.activeIndex === "1") {
        const { data: res } = await this.$http.get(
          `categories/${this.cateId}/attributes`,
          { params: { sel: "many" } }
        );
        console.log(res);
        if (res.meta.status !== 200) {
          return this.$message.error("获取商品数据失败");
        }
        // 处理数据 把attr_vals变为数组
        res.data.forEach((element) => {
          element.attr_vals =
            element.attr_vals.length == 0 ? [] : element.attr_vals.split(" ");
        });
        this.manyTableData = res.data;
        console.log(this.manyTableData);
      } else if (this.activeIndex === "2") {
        const { data: res } = await this.$http.get(
          `categories/${this.cateId}/attributes`,
          { params: { sel: "only" } }
        );
        console.log(res);
        if (res.meta.status !== 200) {
          return this.$message.error("获取商品数据失败");
        }
        // 处理数据 把attr_vals变为数组
        res.data.forEach((element) => {
          element.attr_vals =
            element.attr_vals.length == 0 ? [] : element.attr_vals.split(" ");
        });
        this.onlyTableData = res.data;
        console.log(this.onlyTableData);
      }
    },
  },
  computed: {
    // 封装好的商品id
    cateId() {
      if (this.addform.goods_cat.length === 3) {
        return this.addform.goods_cat[2];
      }
      return null;
    },
  },
};
</script>

<style lang="less" scoped>
.el-checkbox {
  margin: 0 15px 0 0 !important;
}
</style>